package com.hmjk.health.views;

import com.hmjk.health.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Rect;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

public class GlucBarView extends View{
	private Context mContext;
	private float textSize;
	private int greatColor;
	private int goodColor;
	private int badColor;
	private int iGreat, iGood, iBad, total;
	private ShapeDrawable gluc;
	private Paint paintText;
	
	public GlucBarView(Context context) {
		super(context);
		mContext = context;
	}

	public GlucBarView(Context context, AttributeSet attrs) {
		super(context, attrs, android.R.attr.textViewStyle);
		mContext = context;
		init(attrs);
	}
	
	public GlucBarView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		init(attrs);
	}
	
	private void init(AttributeSet attrs){
		TypedArray t = getContext().obtainStyledAttributes(attrs, R.styleable.glucbar);
		textSize = t.getDimension(R.styleable.glucbar_glucTextSize, 16);
		iGreat = 0;
		iGood = 0;
		iBad = 0;
		total = iGreat + iGood + iBad;
		
		greatColor = mContext.getResources().getColor(R.color.FOREGROUND_BLUE);
		goodColor = mContext.getResources().getColor(R.color.FOREGROUND_YELLOW_MID);
		badColor = mContext.getResources().getColor(R.color.FOREGROUND_RED);
		
		gluc = new ShapeDrawable(new RectShape());
		paintText = new TextPaint();
		paintText.setTextSize(textSize);
		paintText.setTypeface(Typeface.DEFAULT_BOLD);
		Rect bounds = new Rect();
		paintText.getTextBounds("8", 0, 1, bounds);
		minTextWidth = bounds.width();
		
	}
	
	float minTextWidth;
	float widthGreat, widthGood, widthBad;
    
	public void setGlucValues(int great, int good, int bad){
		iGreat = great;
		iGood = good;
		iBad = bad;
		total = iGreat + iGood + iBad;
		invalidate();
	}
	
	private void drawGlucBar(Canvas canvas){
		int h = getHeight();   
        int w = getWidth();
        if(total == 0){
        	widthGreat = w;
        }else {
        	widthGreat = w * iGreat/total;
    		widthGood = w * iGood/total;
    		widthBad = w * iBad/total;
		}
		
        int right = 0;
        String paintString = String.valueOf(iGreat);
        Rect bounds = new Rect();

        if((iGreat == total) || (iGood == total) || (iBad == total)){
        	int color = greatColor;
        	if(iGreat == total){
        		color = greatColor;
        		paintString = String.valueOf(iGreat);
        	}else if(iGood == total){
        		color = goodColor;
        		paintString = String.valueOf(iGood);
        	}else if(iBad == total){
        		color = badColor;
        		paintString = String.valueOf(iBad);
        	}
        	
        	gluc.getPaint().setColor(color);
        	gluc.setBounds(0, 0, w, h);
        	gluc.draw(canvas);
        	
        	paintText.getTextBounds(paintString, 0, paintString.length(), bounds);
        	FontMetricsInt fontMetrics = paintText.getFontMetricsInt();
        	int baseline = (h - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
        	canvas.drawText(paintString, w/2 - bounds.width()/2, baseline, paintText);
        	return;
        }
        	        
        if(iGreat != 0){
        	int len = (int)widthGreat;
        	gluc.getPaint().setColor(greatColor);
        	gluc.setBounds(right, 0, right + len, h);
        	gluc.draw(canvas);
        	if(widthGreat > minTextWidth){
	        	paintString = String.valueOf(iGreat);
	        	paintText.getTextBounds(paintString, 0, paintString.length(), bounds);
	        	FontMetricsInt fontMetrics = paintText.getFontMetricsInt();
	        	int baseline = (h - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
	        	canvas.drawText(paintString, right + len/2 - bounds.width()/2, baseline, paintText);
        	}
        	right += len;
        }
        if(iGood != 0){
        	int len = (int)widthGood;
        	gluc.getPaint().setColor(goodColor);
        	gluc.setBounds(right, 0, right + len, h);
        	gluc.draw(canvas);
        	if(widthGood > minTextWidth){
	        	paintString = String.valueOf(iGood);
	        	paintText.getTextBounds(paintString, 0, paintString.length(), bounds);
	        	FontMetricsInt fontMetrics = paintText.getFontMetricsInt();
	        	int baseline = (h - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
	        	canvas.drawText(paintString, right + len/2 - bounds.width()/2, baseline, paintText);
        	}
        	right += len;
        }
        if(iBad != 0){
        	int len = (int)widthBad;
        	gluc.getPaint().setColor(badColor);
        	gluc.setBounds(right, 0, right + len, h);
        	gluc.draw(canvas);
        	if(widthBad > minTextWidth){
	        	paintString = String.valueOf(iBad);
	        	paintText.getTextBounds(paintString, 0, paintString.length(), bounds);
	        	FontMetricsInt fontMetrics = paintText.getFontMetricsInt();
	        	int baseline = (h - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
	        	canvas.drawText(paintString, right + len/2 - bounds.width()/2, baseline, paintText);
        	}
        }
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		drawGlucBar(canvas);
		//Log.d("HealthCardFragment", "GlucBarView onDraw");
	}

}
